package com.datamation.swadeshi.view;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.datamation.swadeshi.R;
import com.datamation.swadeshi.control.data.SalRepDS;
import com.datamation.swadeshi.control.data.SharedPreferencesClass;
import com.datamation.swadeshi.model.SalRep;

public class LoginActivity extends Activity implements OnClickListener{

	EditText username,password;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
		setContentView(R.layout.login);
		
		username = (EditText) findViewById(R.id.editText1);
		password = (EditText) findViewById(R.id.editText2);
		Button login = (Button) findViewById(R.id.btnlogin); 
		
		//SetTitleAdapter titleAdapter=new SetTitleAdapter();
		//titleAdapter.setTitleBarColor(LoginActivity.this);
		
		login.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnlogin:
		{	

			//LoginValidation();
			
			SharedPreferencesClass.setLocalSharedPreference(getApplicationContext(), "first_login","Success");
			Intent mainActivity = new Intent(this, MainActivity.class);
			startActivity(mainActivity);
			finish();

		}	
		break;

		default:
			break;
		}
	}

	private void LoginValidation() {
		// TODO Auto-generated method stub
		SalRepDS ds= new SalRepDS(getApplicationContext());
		ArrayList<SalRep> list=ds.getSaleRepDetails();
		for (SalRep salRep : list) {
			
			if(salRep.getFSALREP_REP_CODE().equals(username.getText().toString().toUpperCase()) 
					&& salRep.getFSALREP_PASSWORD().equals(password.getText().toString().toUpperCase())){
				
				SharedPreferencesClass.setLocalSharedPreference(getApplicationContext(), "first_login","Success");
				Intent mainActivity = new Intent(this, MainActivity.class);
				startActivity(mainActivity);
				finish();
				
			}else{
				Toast.makeText(getApplicationContext(), "Invalid username or password.", Toast.LENGTH_LONG).show();
			
			}
		}
	}

	
	
	
}
